package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.model.Issue;
import com.cskaoyan.model.IssueExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.vo.IssueDataVO;
import com.cskaoyan.service.IssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: IssueServiceImpl
 * @Description: TODO 通用问题模块service实现类,完成具体业务
 * @Author:zk
 * @Date: 2022/5/5 20:28
 * @Version: 1.0
 **/
@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    IssueMapper issueMapper;


    /**
     * @author: zk
     * @description: TODO 查询通用问题列表
     * @date: 2022/5/5 21:29
     * @Param: question,baseParamVO
     * @return IssueDataVO
     */
    @Override
    public IssueDataVO query(String question, BaseParamBO param) {
        Integer limit = param.getLimit();//页面容量
        Integer page = param.getPage();//页码
        String sort = param.getSort();//排序列(条件)
        String order = param.getOrder();//排序方式desc或asc
        //分页
        PageHelper.startPage(page,limit);
        //执行查询:仅查询deleted为0,即未被逻辑删除的
        List<Issue> list = issueMapper.selectIssues(question,sort,order);
        //传入查询结果通过PageHelper获取total和pages
        PageInfo<Issue> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return IssueDataVO.data(total,pages,limit,page,list);
    }

    /**
     * @author: zhoukang
     * @description: TODO 新增通用问题
     * @date: 2022/5/5 23:08
     */
    @Override
    public Issue insert(String question, String answer) {
        Date date = new Date();
        Issue issue = new Issue();

        issue.setQuestion(question);
        issue.setAnswer(answer);
        issue.setAddTime(date);
        issue.setUpdateTime(date);
        issue.setDeleted(false);

        int id = issueMapper.insertIssue(issue);
        return issue;
    }

    /**
     * @author: zhoukang
     * @description: TODO 逻辑删除通用问题
     * @date: 2022/5/6 11:49
     * @Param: id
     * @return
     */
    @Override
    public int delete(Issue issue) {
        //设置逻辑删除为true
        issue.setDeleted(true);
        //更新时间
        issue.setUpdateTime(new Date());
        int row = issueMapper.updateByPrimaryKey(issue);
        return row;
    }

    /**
     * @author: zhoukang
     * @description: TODO 更新通用问题信息
     * @date: 2022/5/6 13:08
     * @Param:
     * @return
     */
    @Override
    public Issue update(Issue issue) {
        //更新时间
        issue.setUpdateTime(new Date());
        //根据id更新相应问题
        issueMapper.updateByPrimaryKey(issue);
        return issue;
    }

    /**
     * @author: zk
     * @description: TODO 查询所有未被逻辑删除的问题
     * @date: 2022/5/10 10:11
     */
    @Override
    public DataVO<Issue> queryAllIssues(BaseParamBO param) {
        Integer page = param.getPage();
        Integer limit = param.getLimit();

        param.setSort("add_time");
        param.setOrder("desc");
        String sort = param.getSort();
        String order = param.getOrder();

        //分页
        PageHelper.startPage(page,limit);

        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort+" "+order);
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Issue> list = issueMapper.selectByExample(issueExample);

        PageInfo<Issue> issuePageInfo = new PageInfo<>(list);
        long total = issuePageInfo.getTotal();
        int pages = issuePageInfo.getPages();
        return DataVO.data(total,pages,limit,page,list);
    }
}
